<template>
  <div class="member-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>会员管理</span>
        </div>
      </template>

      <!-- 搜索区域 -->
      <div class="search-area">
        <el-form :model="searchForm" :inline="true" class="search-form">
          <el-form-item label="会员名称">
            <el-input
              v-model="searchForm.memberName"
              placeholder="请输入会员名称"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="手机号">
            <el-input
              v-model="searchForm.phone"
              placeholder="请输入手机号"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="会员等级">
            <el-select
              v-model="searchForm.level"
              placeholder="请选择会员等级"
              clearable
              style="width: 150px"
            >
              <el-option label="普通会员" :value="1" />
              <el-option label="银牌会员" :value="2" />
              <el-option label="金牌会员" :value="3" />
              <el-option label="钻石会员" :value="4" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select
              v-model="searchForm.status"
              placeholder="请选择状态"
              clearable
              style="width: 120px"
            >
              <el-option label="正常" :value="1" />
              <el-option label="禁用" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">
              <el-icon><Search /></el-icon>
              搜索
            </el-button>
            <el-button @click="handleReset">
              <el-icon><Refresh /></el-icon>
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 会员列表 -->
      <el-table
        :data="memberList"
        stripe
        border
        style="width: 100%"
      >
        <el-table-column prop="memberName" label="会员名称" width="120" />
        <el-table-column prop="phone" label="手机号" width="130" />
        <el-table-column prop="email" label="邮箱" width="200" />
        <el-table-column prop="level" label="会员等级" width="100">
          <template #default="{ row }">
            <el-tag :type="getLevelType(row.level)">
              {{ getLevelText(row.level) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="points" label="积分" width="100" />
        <el-table-column prop="totalAmount" label="累计消费" width="120">
          <template #default="{ row }">
            ¥{{ row.totalAmount }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="80">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.status === 1 ? '正常' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="lastLoginTime" label="最后登录" width="180" />
        <el-table-column prop="createTime" label="注册时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleView(row)">
              查看
            </el-button>
            <el-button type="success" size="small" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="warning" size="small" @click="handlePoints(row)">
              积分
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <div class="pagination-area">
        <el-pagination
          :current-page="pagination.pageNum"
          :page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Search, Refresh } from '@element-plus/icons-vue'

// 会员数据接口
interface Member {
  id: number
  memberName: string
  phone: string
  email: string
  level: number
  points: number
  totalAmount: number
  status: number
  lastLoginTime: string
  createTime: string
}

// 搜索表单
const searchForm = reactive({
  memberName: '',
  phone: '',
  level: undefined,
  status: undefined
})

// 分页信息
const pagination = reactive({
  pageNum: 1,
  pageSize: 10
})

const total = ref(0)

// 会员列表
const memberList = ref<Member[]>([
  {
    id: 1,
    memberName: '张三',
    phone: '13800138001',
    email: 'zhangsan@example.com',
    level: 3,
    points: 2580,
    totalAmount: 12580.00,
    status: 1,
    lastLoginTime: '2024-01-01 10:30:00',
    createTime: '2023-06-01 09:00:00'
  },
  {
    id: 2,
    memberName: '李四',
    phone: '13800138002',
    email: 'lisi@example.com',
    level: 2,
    points: 1200,
    totalAmount: 6800.00,
    status: 1,
    lastLoginTime: '2024-01-01 11:15:00',
    createTime: '2023-08-15 14:20:00'
  },
  {
    id: 3,
    memberName: '王五',
    phone: '13800138003',
    email: 'wangwu@example.com',
    level: 1,
    points: 350,
    totalAmount: 2350.00,
    status: 1,
    lastLoginTime: '2024-01-01 14:20:00',
    createTime: '2023-12-01 16:30:00'
  },
  {
    id: 4,
    memberName: '赵六',
    phone: '13800138004',
    email: 'zhaoliu@example.com',
    level: 4,
    points: 5600,
    totalAmount: 25600.00,
    status: 1,
    lastLoginTime: '2024-01-01 15:30:00',
    createTime: '2023-03-10 10:15:00'
  }
])

// 获取会员等级类型
const getLevelType = (level: number) => {
  const types = ['', 'info', 'warning', 'success', 'danger']
  return types[level] || 'info'
}

// 获取会员等级文本
const getLevelText = (level: number) => {
  const texts = ['', '普通会员', '银牌会员', '金牌会员', '钻石会员']
  return texts[level] || '未知'
}

// 搜索
const handleSearch = () => {
  pagination.pageNum = 1
  // 这里可以调用API搜索会员
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    memberName: '',
    phone: '',
    level: undefined,
    status: undefined
  })
  pagination.pageNum = 1
  // 这里可以调用API获取会员列表
}

// 查看会员详情
const handleView = (member: Member) => {
  console.log('查看会员:', member)
}

// 编辑会员
const handleEdit = (member: Member) => {
  console.log('编辑会员:', member)
}

// 积分管理
const handlePoints = (member: Member) => {
  console.log('积分管理:', member)
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  pagination.pageNum = 1
  // 这里可以调用API获取会员列表
}

// 当前页变化
const handleCurrentChange = (page: number) => {
  pagination.pageNum = page
  // 这里可以调用API获取会员列表
}

// 初始化
onMounted(() => {
  total.value = memberList.value.length
})
</script>

<style scoped lang="scss">
.member-list {
  .card-header {
    font-weight: bold;
    color: #333;
  }

  .search-area {
    margin-bottom: 20px;
    padding: 20px;
    background-color: #f5f5f5;
    border-radius: 4px;
  }

  .pagination-area {
    margin-top: 20px;
    text-align: right;
  }
}
</style>

